export default {
  data () {
    return {
      loading: true,
      data: [],
      page: 1,
      pageSize: 10,
      pageTotal: 0,
      ruleForm: {},
      resetFields: {},
      showDialog: false,
      rules: {}
    }
  },

  methods: {
    // 加载数据
    loadData () {
      this.$axios.get(`${this.url}?page=${this.page}&page_size=${this.pageSize}`)
        .then(response => {
          this.loading = false
          if (response.data.err_code) {
            this.$message.warning(response.data.message)
          } else {
            this.pageTotal = response.data.total
            this.data = response.data.list
          }
        })
        .catch((e) => {
          this.loading = false
          this.$message.error('系统错误')
        })
    },

    // 切换页码
    changePage (page) {
      this.page = page

      this.loadData()
    },

    // 删除
    del (id) {
      this.$confirm('确认删除这条记录么？', '提示', {
        type: 'warning',
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        lockScroll: false
      }).then(() => {
        this.$axios.delete(`${this.delUrl}/${id}`)
          .then((response) => {
            if (!response.data.err_code) {
              this.$message({
                type: 'success',
                message: '删除成功',
                duration: 1000,
                onClose: () => {
                  this.loadData()
                }
              })
            } else {
              this.$message.warning(response.data)
            }
          })
      }).catch(() => { })
    },

    // 提交
    submit (ruleForm) {
      this.$refs[ruleForm].validate(valid => {
        if (valid) {
          this.loading = true
          let data = JSON.parse(JSON.stringify(this.ruleForm))
          if (data.pid) {
            data.pid = typeof data.pid === 'object' ? data.pid.pop() : data.pid
          }
          let url = data.id ? `${this.editUrl}/${data.id}` : this.addUrl
          this.$axios.post(url, data)
            .then(response => {
              this.loading = false
              this.loadData()
              this.showDialog = false
              if (!response.data.err_code) {
                this.$message({
                  type: 'success',
                  message: '操作成功',
                  duration: 1000,
                  onClose: () => {
                    this.$refs.ruleForm.resetFields()
                  }
                })
              } else {
                this.loading = false
                this.$message.warning(response.data.message)
              }
            })
            .catch(() => {
              this.$message.error('系统错误')
            })
        }
      })

      return false
    },

    // 编辑弹窗
    edit (row) {
      this.ruleForm = row
      this.showDialog = true
    },

    // 添加
    add () {
      this.ruleForm = JSON.parse(JSON.stringify(this.resetFields))
      this.showDialog = true
    },

    // 获取规则
    loadRules () {
      this.$axios.get('/admin/rule/tree')
        .then(response => {
          if (!response.data.err_code) {
            if (this.options.length !== 0) {
              this.options = [this.options, ...response.data]
            } else {
              this.options = response.data
            }
          } else {
            this.$message.warning(response.message)
          }
        })
        .catch((e) => {
          this.$message.error('系统错误')
        })
    },

    loadRoles () {
      this.$axios.get('/admin/roles?page_size=1000')
        .then(response => {
          if (!response.data.err_code) {
            this.options = response.data.list
          } else {
            this.$message.warning(response.data.message)
          }
        })
        .catch(() => {
          this.$message.error('系统错误')
        })
    },

    refresh () {
      this.loading = true
      this.loadData()
    }
  },

  computed: {
    resourceUrl () {
      return process.env.VUE_APP_BASE_URL
    }
  }
}
